package internal

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
	"time"
	"wms/util"
)

// const consulUrl string = "http://127.0.0.1:8500/v1/kv/wms"
// http://consul.dev.jiuxiniot.com/v1/kv/config/jx-gateway-wms
// "PARAM=--spring.cloud.consul.host=consul.dev-middeware --spring.cloud.consul.port=8500 --server.port=80 --env=DEV --apollo.meta=http://192.168.1.81:8080"

var consulUrl string = "" //http://consul.dev-middeware:8500/v1/kv/config/jx-gateway-wms"

func readConsul() []byte {
	t1 := time.Now()

	param := os.Getenv("PARAM")
	// param
	// "--spring.cloud.consul.host=consul.dev-middeware --spring.cloud.consul.port=8500 --server.port=80 --env=DEV --apollo.meta=http://192.168.1.81:8080"
	if len(param) > 0 {
		//

		//--spring.cloud.consul.host=consul.dev-middeware --spring.cloud.consul.port=8500 --server.port=80 --env=DEV --apollo.meta=http://192.168.1.81:8080
		//consul url:http://consul.dev-middeware:8500/v1/kv/config/jx-gateway-wms

		util.Log(param)
		// "PARAM=--spring.cloud.consul.host=consul.dev-middeware --spring.cloud.consul.port=8500 --server.port=80 --env=DEV --apollo.meta=http://192.168.1.81:8080"
		//mapKv := make(map[string]string, 10)
		//if len(param) > 0 {
		//	fields := strings.Split(param, " ") //strings.FieldsFunc(param, unicode.IsSpace)
		//	//
		//	for _, field := range fields {
		//		curKV := strings.Split(field, "=")
		//		if len(field) > 1 {
		//			mapKv[curKV[0]] = curKV[1]
		//		}
		//	}
		//}
		//consulHost := mapKv["--spring.cloud.consul.host"]
		//consulPort := mapKv["--spring.cloud.consul.port"]
		// consulUrl = fmt.Sprintf("http://%s:%s/v1/kv/config/jx-gateway-wms", consulHost, consulPort)
		consulUrl = "http://consul.dev-middeware:8500/v1/kv/config/jx-gateway-wms"

	} else {
		// 本地的
		consulUrl = "http://127.0.0.1:8500/v1/kv/wms"
	}
	util.Log("consul url:" + consulUrl)
	resp, err := http.Get(consulUrl)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	d1, _ := io.ReadAll(resp.Body)
	util.Log("consul code:"+resp.Status+";data:", string(d1))
	r1 := bytes.NewReader(d1)
	var entries []*KVPair
	// json.Unmarshal()
	dec := json.NewDecoder(r1)
	err = dec.Decode(&entries)
	if err != nil {
		panic(err)
	}
	if len(entries) == 0 {
		panic("entries len is 0")
	}
	fmt.Printf("请求时间:%d微秒\n", time.Since(t1).Microseconds())
	return entries[0].Value
}

// KVPair
type KVPair struct {
	// Key is the name of the key. It is also part of the URL path when accessed via the API.
	Key string

	// CreateIndex holds the index corresponding the creation of this KVPair. This is a read-only field.
	CreateIndex uint64

	// ModifyIndex is used for the Check-And-Set operations and can also be fed
	// back into the WaitIndex of the QueryOptions in order to perform blocking
	// queries.
	ModifyIndex uint64

	// LockIndex holds the index corresponding to a lock on this key, if any. This
	// is a read-only field.
	LockIndex uint64

	// Flags are any user-defined flags on the key. It is up to the implementer to check these values, since Consul does not treat them specially.
	Flags uint64

	// Value is the value for the key. This can be any value, but it will be base64 encoded upon transport.
	Value []byte

	// Session is a string representing the ID of the session. Any other interactions with this key over the same session must specify the same session ID.
	Session string
}
